home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / amassm.lha / 68000MacLanguage.Course2.pp / 68000MacLanguage.Course2
Encoding:
Text File  |  1990-09-07  |  49.0 KB  |  1,565 lines

  1.  
  2.   Another great doc for the budding assembly langage programmers.
  3.          Part 6-10 of of the 68000 Machine Language Course
  4.                   Brought to you by Sewer Software
  5.  
  6.  
  7.  
  8. ASSEMBLY LANGUAGE COURSE PART VI by Mark van den Boer
  9.  
  10. Shift and Rotate Operations
  11.  
  12. Instruction:   ASL
  13. Syntax:        ASL #,Dn (the immediate operand always modulo 8)
  14.                ASL Dn,Dn (the first operand always modulo 8)
  15.                ASL <ea>
  16. Data sizes:    byte,  word,  long  except for ASL <ea> which  only
  17.                allows word and long as data sizes.
  18. Condition codes affected:
  19.                X    set to the last bit shifted out
  20.                N    set to the most significant bit of the result
  21.                Z    set if the result is zero, cleared otherwise
  22.                V    set  if  the most significant bit  is  changed
  23.                     during the operation
  24.                C    see the X-bit
  25.  
  26.  
  27.  
  28.  
  29.  
  30. Addressing modes allowed with the ASL <ea> instruction:
  31. Destination:
  32.           (An)
  33.           (An)+
  34.           -(An)
  35.           w(An)
  36.           b(An,Rn)
  37.           w
  38.           l
  39. Function: Perform  a shift left of the destination  operand.  This
  40.           instruction can be used as a fast form of multiplying an
  41.           operand  with a power of two.  On a processor  like  the
  42.           6502  this  instruction  is the only way  to  perform  a
  43.           multiply operation.  The lower bit of the destination is
  44.           always set to zero.
  45. Examples:
  46. Instruction              Before         After
  47. ASL.L d0,d1              d0=33333333    d0=33333333
  48.                          d1=00000005    d1=00000028
  49. ASL.W $4ee               $4ee=0009      $4ee=0012
  50.  
  51.  
  52. Instruction:   ASR
  53. Syntax:        ASR #,Dn (the immediate operand always modulo 8)
  54.                ASR Dn,Dn (the first operand always modulo 8)
  55.                ASR <ea>
  56. Data sizes:    byte,  word,  long  except for ASR <ea> which  only
  57.                allows word and long as data sizes.
  58. Condition codes affected:
  59.                X    set to the last bit shifted out
  60.                N    set to the most significant bit of the result
  61.                Z    set if the result is zero, cleared otherwise
  62.                V    set  if  the most significant bit  is  changed
  63.                     during the operation
  64.                C    see the X-bit
  65. Addressing modes allowed with the ASR <ea> instruction:
  66. Destination:
  67.           (An)
  68.           (An)+
  69.           -(An)
  70.           w(An)
  71.           b(An,Rn)
  72.           w
  73.           l
  74. Function: Perform a shift right of the destination  operand.  This
  75.           instruction  can be used as a fast form of  dividing  an
  76.           operand  with a power of two.  On a processor  like  the
  77.           6502  this  instruction  is the only way  to  perform  a
  78.           divide  operation.  The upper bit (sign bit)  is  always
  79.           repeated.
  80. Examples:
  81. Instruction              Before         After
  82. ASR.L d0,d1              d0=33333333    d0=33333333
  83.                          d1=00000005    d1=00000002
  84. ASR.W $4ee               $4ee=8009      $4ee=c004
  85.  
  86.  
  87. Instruction:   LSL
  88. See the ASL instruction.  The LSL instruction is exactly the same.
  89. At the moment I haven't got the machine codes for the ASL and LSL
  90. operations  but I think that even the machine codes are the  same.
  91. E.g.  on the 6809 both ASL and LSL exist but translate to the same
  92. machine code.
  93.  
  94.  
  95.  
  96. Instruction:   LSR
  97. Syntax:        LSR #,Dn (the immediate operand always modulo 8)
  98.                LSR Dn,Dn (the first operand always modulo 8)
  99.                LSR <ea>
  100. Data sizes:    byte,  word,  long  except for LSR <ea> which  only
  101.                allows word and long as data sizes.
  102. Condition codes affected:
  103.                X    set to the last bit shifted out
  104.                N    set to the most significant bit of the result
  105.                Z    set if the result is zero, cleared otherwise
  106.                V    set  if  the most significant bit  is  changed
  107.                     during the operation
  108.                C    see the X-bit
  109. Addressing modes allowed with the LSR <ea> instruction:
  110. Destination:
  111.           (An)
  112.           (An)+
  113.           -(An)
  114.           w(An)
  115.           b(An,Rn)
  116.           w
  117.           l
  118. Function: Perform a shift right of the destination  operand.  This
  119.           instruction differs from ASR in that the high order  bit
  120.           is always cleared.
  121. Examples:
  122. Instruction              Before         After
  123. LSR.L d0,d1              d0=33333333    d0=33333333
  124.                          d1=00000005    d1=00000002
  125. LSR.W $4ee               $4ee=0009      $4ee=0004
  126.  
  127.  
  128. Instruction:   ROL
  129. Syntax:        ROL #,Dn (the immediate operand always modulo 8)
  130.                ROL Dn,Dn (the first operand always modulo 8)
  131.                ROL <ea>
  132. Data sizes:    byte, word, long
  133. Condition codes affected:
  134.                X    not affected
  135.                N    set to the most significant bit of the result
  136.                Z    set if the result is zero, cleared otherwise
  137.                V    always cleared
  138.                C    set to the last bit shifted out the operand
  139.  
  140. Addressing modes allowed with the ROL <ea> instruction:
  141. Destination:
  142.           (An)
  143.           (An)+
  144.           -(An)
  145.           w(An)
  146.           b(An,Rn)
  147.           w
  148.           l
  149. Function: Perform  a  bitwise  rotate  left  of  the   destination
  150.           operand.
  151. Examples:
  152. Instruction              Before         After
  153. ROL.L d0,d1              d0=00000001    d0=00000001
  154.                          d1=88000001    d1=10000002  (C bit set)
  155. ROL.W $4ee               $4ee=8009      $4ee=0012
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. Instruction:   ROR
  163. Syntax:        ROR #,Dn (the immediate operand always modulo 8)
  164.                ROR Dn,Dn (the first operand always modulo 8)
  165.                ROR <ea>
  166. Data sizes:    byte, word, long
  167. Condition codes affected:
  168.                X    not affected
  169.                N    set to the most significant bit of the result
  170.                Z    set if the result is zero, cleared otherwise
  171.                V    always cleared
  172.                C    set to the last bit shifted out the operand
  173. Addressing modes allowed with the ROR <ea> instruction:
  174. Destination:
  175.           (An)
  176.           (An)+
  177.           -(An)
  178.           w(An)
  179.           b(An,Rn)
  180.           w
  181.           l
  182. Function: Perform  a  bitwise  rotate  right  of  the  destination
  183.           operand.
  184. Examples:
  185. Instruction              Before         After
  186. ROR.L d0,d1              d0=00000001    d0=00000001
  187.                          d1=88000001    d1=c4000000  (C bit set)
  188. ROR.W $4ee               $4ee=8009      $4ee=c004
  189.  
  190.  
  191. Instruction:   ROXL
  192. Syntax:        ROXL #,Dn (the immediate operand always modulo 8)
  193.                ROXL Dn,Dn (the first operand always modulo 8)
  194.                ROXL <ea>
  195. Data sizes:    byte, word, long
  196. Condition codes affected:
  197.                X    set to the last bit shifted out the operand
  198.                N    set to the most significant bit of the result
  199.                Z    set if the result is zero, cleared otherwise
  200.                V    always cleared
  201.                C    set to the last bit shifted out the operand
  202.  
  203.  
  204.  
  205.  
  206. Addressing modes allowed with the ROXL <ea> instruction:
  207. Destination:
  208.           (An)
  209.           (An)+
  210.           -(An)
  211.           w(An)
  212.           b(An,Rn)
  213.           w
  214.           l
  215. Function: Perform  a  bitwise  rotate  left  of  the   destination
  216.           operand.  There  is very little difference with the  ROL
  217.           instruction.  By  the way,  it is very handy to  have  a
  218.           wordprocessor    with   cut/paste    and    find/replace
  219.           facilities.  All  I  did was cut out  the  complete  ROL
  220.           instruction and replaced all ROL's by ROXL's.
  221. Examples:
  222. Instruction              Before         After
  223. ROXL.L d0,d1             d0=00000001    d0=00000001
  224.                          d1=88000001    d1=10000002
  225. ROXL.W $4ee              $4ee=8009      $4ee=0012
  226.  
  227.  
  228. Instruction:   ROXR
  229. Syntax:        ROXR #,Dn (the immediate operand always modulo 8)
  230.                ROXR Dn,Dn (the first operand always modulo 8)
  231.                ROXR <ea>
  232. Data sizes:    byte, word, long
  233. Condition codes affected:
  234.                X    set to the last bit shifted out the operand
  235.                N    set to the most significant bit of the result
  236.                Z    set if the result is zero, cleared otherwise
  237.                V    always cleared
  238.                C    set to the last bit shifted out the operand
  239. Addressing modes allowed with the ROXR <ea> instruction:
  240. Destination:
  241.           (An)
  242.           (An)+
  243.           -(An)
  244.           w(An)
  245.           b(An,Rn)
  246.           w
  247.           l
  248.  
  249.  
  250. Function: Perform  a  bitwise  rotate  right  of  the  destination
  251.           operand.  There  is very little difference with the  ROR
  252.           instruction.  By  the way,  it is very handy to  have  a
  253.           wordprocessor    with   cut/paste    and    find/replace
  254.           facilities.  All  I  did was cut out the  complete  ROXL
  255.           instruction and replaced all ROXL's by ROXR's.
  256. Examples:
  257. Instruction              Before         After
  258. ROXR.L d0,d1             d0=00000001    d0=00000001
  259.                          d1=88000001    d1=10000002
  260. ROXR.W $4ee              $4ee=8009      $4ee=0012
  261.  
  262.  
  263.  
  264. MC 68000 MACHINE LANGUAGE COURSE PART VII by Mark van den Boer
  265.  
  266. I would like to dedicate this part to Willeke,  who gives  Richard 
  267. sleepless  nights and the inspiration to write even more  exciting 
  268. issues of ST NEWS.  I only saw Willeke on photograph, but she must 
  269. be  a fine girl.  In my opinion there are three qualities which  a 
  270. girl  must have,  to qualify as a fine girl.  These are:  1)  like 
  271. Queensr˜che,  2) like ST NEWS (no,  she doesn't have to like  this 
  272. particular machine language course).
  273. Now,  you're all anxious to know the third quality, aren't you? If 
  274. you think you know the third one,  send your answer to ST NEWS.  A 
  275. bottle  of  wine will be raffled among the persons  who  gave  the 
  276. right answer.  There will be another bottle for the most  original 
  277. answer!
  278.  
  279. Bit Manipulation instructions
  280.  
  281. Instruction:   BTST
  282. Syntax:        BTST Dn,<ea> or BTST #,<ea>
  283. Data sizes:    only byte when <ea> is an address.  Only long  when 
  284.                <ea> is a data register.
  285.  
  286. Condition codes affected:
  287.                X    not affected
  288.                N    not affected
  289.                Z    set if the result is zero, cleared otherwise
  290.                V    not affected
  291.                C    not affected
  292. Addressing modes allowed:
  293. Destination:
  294.           Dn
  295.           (An)
  296.           (An)+
  297.           -(An)
  298.           w(An)
  299.           b(An,Rn)
  300.           w
  301.           l
  302.           w(PC)
  303.           b(PC,Rn)
  304.           #         (only when source is Dn)
  305.  
  306.  
  307.  
  308. Function: Test a single bit of an effective address operand.  Bits 
  309.           are  numbered  from  0  to 31,  where  0  is  the  least 
  310.           significant bit (you could use this instruction to  test 
  311.           if  a number is odd).  This instruction is  useful  when 
  312.           specific  bits of an operand have to  be  checked.  E.g. 
  313.           when reading joystick information one could test with  a 
  314.           single  instruction whether the fire-button was  pressed 
  315.           or not.  Compared to the 6502 and 6809 this  instruction 
  316.           (in  fact all bit manipulation instructions) are a  step 
  317.           forward,  since  with these older processors one had  to 
  318.           put  the data in a register first,  then filter the  bit 
  319.           with  an AND-operation and then the Z-bit in the  status 
  320.           register  was at last set.  Viva el 68000!!  Since  this 
  321.           instruction has the rather odd property of only  working 
  322.           on  byte  and  long operands it is  important  that  you 
  323.           remember   what  I  wrote  in  a  previous  part   about 
  324.           specifying data sizes.
  325.  
  326.  
  327.  
  328.  
  329.  
  330. Examples:
  331. Instruction              Before              After
  332. BTST.B #5,$345678        $345678             $345678 contains
  333.                          contains $78        $78
  334.                                              Z-bit is 1
  335. BTST.L d0,d1             d0=0                d0=0
  336.                          d1=$12345678        d1=$12345678
  337.                                              Z-bit is 0
  338.  
  339.  
  340. Instruction:   BCLR
  341. Syntax:        BTST Dn,<ea> or BTST #,<ea>
  342. Data sizes:    only byte when <ea> is an address.  Only long  when 
  343.                <ea> is a data register.
  344. Condition codes affected:
  345.                X    not affected
  346.                N    not affected
  347.                Z    set if the result is zero, cleared otherwise
  348.                V    not affected
  349.                C    not affected
  350.  
  351.  
  352. Addressing modes allowed:
  353. Destination:
  354.           Dn
  355.           (An)
  356.           (An)+
  357.           -(An)
  358.           w(An)
  359.           b(An,Rn)
  360.           w
  361.           l
  362.           w(PC)
  363.           b(PC,Rn)
  364.           #         (only when source is Dn)
  365. Function: Bit  test and CLeaR.  First tests the bit to be  cleared 
  366.           and  sets  the  Z-bit  accordingly,   then  clears   the 
  367.           specified bit.
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374. Examples:
  375. Instruction              Before              After
  376. BCLR.B #5,$345678        $345678             $345678 contains
  377.                          contains $78        $58
  378.                                              Z-bit is 1
  379. BCLR.L d0,d1             d0=0                d0=0
  380.                          d1=$12345678        d1=$12345678
  381.                                              Z-bit is 0
  382.  
  383.  
  384. Instruction:   BSET
  385. Syntax:        BSET Dn,<ea> or BSET #,<ea>
  386. Data sizes:    only byte when <ea> is an address.  Only long  when 
  387.                <ea> is a data register.
  388. Condition codes affected:
  389.                X    not affected
  390.                N    not affected
  391.                Z    set if the result is zero, cleared otherwise
  392.                V    not affected
  393.                C    not affected
  394.  
  395.  
  396. Addressing modes allowed:
  397. Destination:
  398.           Dn
  399.           (An)
  400.           (An)+
  401.           -(An)
  402.           w(An)
  403.           b(An,Rn)
  404.           w
  405.           l
  406.           w(PC)
  407.           b(PC,Rn)
  408.           #         (only when source is Dn)
  409. Function: Bit test and SET. First tests the bit to be set and sets 
  410.           the Z-bit accordingly, then sets the specified bit. This 
  411.           instruction  and  the BCLR instruction can  be  used  as 
  412.           alternatives to the TAS-instruction.
  413.  
  414.  
  415.  
  416.  
  417.  
  418. Examples:
  419. Instruction              Before              After
  420. BSET.B #5,$345678        $345678             $345678 contains
  421.                          contains $78        $78
  422.                                              Z-bit is 1
  423. BSET.L d0,d1             d0=0                d0=0
  424.                          d1=$12345678        d1=$12345679
  425.                                              Z-bit is 0
  426.  
  427.  
  428. Instruction:   BCHG
  429. Syntax:        BCHG Dn,<ea> or BCHG #,<ea>
  430. Data sizes:    only byte when <ea> is an address.  Only long  when 
  431.                <ea> is a data register.
  432. Condition codes affected:
  433.                X    not affected
  434.                N    not affected
  435.                Z    set if the result is zero, cleared otherwise
  436.                V    not affected
  437.                C    not affected
  438.  
  439.  
  440. Addressing modes allowed:
  441. Destination:
  442.           Dn
  443.           (An)
  444.           (An)+
  445.           -(An)
  446.           w(An)
  447.           b(An,Rn)
  448.           w
  449.           l
  450.           w(PC)
  451.           b(PC,Rn)
  452.           #         (only when source is Dn)
  453. Function: Bit test and CHanGe.  First tests the bit to be  changed 
  454.           and  sets  the  Z-bit  accordingly,   then  changes  the 
  455.           specified bit.
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462. Examples:
  463. Instruction              Before              After
  464. BCHG.B #5,$345678        $345678             $345678 contains
  465.                          contains $78        $58
  466.                                              Z-bit is 1
  467. BCHG.L d0,d1             d0=0                d0=0
  468.                          d1=$12345678        d1=$12345679
  469.                                              Z-bit is 0
  470.  
  471.  
  472. Binary Coded Decimal (BCD) instructions
  473.  
  474. To understand this instructionclass we must first know what a BCD-
  475. digit is.  It is a representation of decimal digits in an array of 
  476. bytes  (array may be of length 1 or greater).  In every  byte  the 
  477. decimal  number  0  to 99 can be  represented.  This  is  done  as 
  478. follows:  a  byte can be divided into two four-bit  parts,  called 
  479. nibbles.  In every nibble,  one decimal digit is represented. This 
  480. implicates that the binary combination 1010 can never occur in BCD 
  481. representation,  since it isn't in the decimal range from 0 to  9. 
  482.  
  483.  
  484. The BCD-representation is especially convenient when printing such 
  485. a digit, since it doesn't take much calculation to convert it to a 
  486. printable character.  A disadvantage of the BCD-representation  is 
  487. that one doesn't use the full storage capacity of a byte or word.
  488. The 68000 has three special BCD-artithmetic instructions.
  489.  
  490.  
  491. Instruction:   ABCD
  492. Syntax:        ABCD Dn,Dn or ABCD -(An),-(An)
  493. Data sizes:    byte
  494. Condition codes affected:
  495.                X    set by carry out of most significant
  496.                     BCD-nibble, cleared otherwise
  497.                N    undefined
  498.                Z    set if the result is zero, cleared otherwise
  499.                V    undefined
  500.                C    same as X-bit
  501. Function: Add two BCD-digits.  The predecremeting addressing  mode 
  502.           has   been  provided  for  computations  with   multiple 
  503.           precision  BCD-numbers.   This  implies  that  the  most 
  504.           significant  BCD-numbers  must be stored  in  the  lower 
  505.           memory addresses.
  506. Examples:
  507. Instruction              Before              After
  508. ABCD.B d0,d1             d0=$53              d0=$53
  509.                          d1=$32              d1=$85
  510.  
  511.  
  512. Instruction:   SBCD
  513. Syntax:        SBCD Dn,Dn or SBCD -(An),-(An)
  514. Data sizes:    byte
  515. Condition codes affected:
  516.                X    set by carry out of most significant
  517.                     BCD-nibble, cleared otherwise
  518.                N    undefined
  519.                Z    set if the result is zero, cleared otherwise
  520.                V    undefined
  521.                C    same as X-bit
  522. Function: Subtract two BCD-digits.
  523. Examples:
  524. Instruction              Before              After
  525. ABCD.B d0,d1             d0=$53              d0=$53
  526.                          d1=$32              d1=$21
  527.  
  528. Instruction:   NBCD
  529. Syntax:        NBCD <ea>
  530. Data sizes:    byte
  531. Addressing modes allowed:
  532.           Dn
  533.           (An)
  534.           (An)+
  535.           -(An)
  536.           w(An)
  537.           b(An,Rn)
  538.           w
  539.           l
  540. Condition codes affected:
  541.                X    set by borrow out of most significant
  542.                     BCD-nibble, cleared otherwise
  543.                N    undefined
  544.                Z    set if the result is zero, cleared otherwise
  545.                V    undefined
  546.                C    same as X-bit
  547.  
  548.  
  549.  
  550. Function: Negate  a  BCD-number.  How  it functions  can  be  best 
  551.           described with an example.  Let's negate $23.  The  NBCD 
  552.           operation yields $77.  Now,  how did we get this result? 
  553.           It's easy, just subtract $23 from $99 and you've got it. 
  554. Examples:
  555. Instruction              Before              After
  556. NBCD.B d0                d0=$43              d0=$56
  557.  
  558.  
  559. This  is  the end of part seven.  Next time I will deal  with  all 
  560. program flow instruction, such as branches and jumps. 
  561.  
  562.  
  563.  
  564. MC 68000 ASSEMBLY LANGUAGE COURSE PART VIII by Mark van den Boer
  565.  
  566. Program Control Instructions
  567.  
  568. This  class of instructions enables a programmer to create  loops 
  569. and  IF-THEN-ELSE  like  decisions.  That's  why  it's  the  most 
  570. important group of instructions and every programmer should  have 
  571. a  thorough knowledge of this group.  This class of  instructions 
  572. are specifically meant to affect the program counter.
  573.  
  574. Instructions:  Bcc  (cc stands for Condition Code)
  575. Syntax:        Bcc  <address>
  576. Data sizes:    Byte  or word.  This implicates that  the  branch-
  577.                instructions  can branch in an area of  32K.  When 
  578.                using a branch with a byte offset you can put a .S 
  579.                suffix behind the instruction e.g.  BEQ.S  .  When 
  580.                using a branch with a word offset you can put a .W 
  581.                suffix behind the instruction e.g.  BEQ.W  .  Most 
  582.                assemblers  will  determine if the short  or  word 
  583.                form is needed. Also most assemblers will optimize 
  584.                word-branches to byte-branches whenever possible.
  585.  
  586. Condition codes affected:
  587.                None
  588. Function: Test  a  combination of the NZVC-flags in  the  status-
  589.           register and conditionally perform a branch to  another 
  590.           address. If the testing of the condition codes is true, 
  591.           then  the  branch  will be  taken,  in  the  other  the 
  592.           instruction  immediately following the Bcc  instruction 
  593.           will  be executed. A total of 15 possible variations of 
  594.           this instruction are listed below.
  595.           BCC: where  CC stands for Carry Clear.  The  branch  is 
  596.                taken if the C-bit is 0. This instruction is often 
  597.                used   in  combination  with  shift   and   rotate 
  598.                instructions.
  599.           BCS: where CS stands for Carry Set. The branch is taken 
  600.                if  the  C-bit  is  1.  This  instruction  is  the 
  601.                counterpart of the BCC-instruction.
  602.           BEQ: where EQ stand for EQual.  The branch is taken  if 
  603.                the  Z-bit is 1.  This instruction is  often  used 
  604.                after a TST-instruction or CMP-instruction.
  605.           BNE: where NE stands for Not Equal. The branch is taken 
  606.                if  the  Z-bit  is  0.  This  instruction  is  the 
  607.                counterpart of the BNE-instruction.
  608.           BPL: where PL stands for PLus.  The branch is taken  if 
  609.                the  N-bit is 0.  This instruction is  often  used 
  610.                after a TST-instruction or CMP-instruction.
  611.           BMI: where MI stands for MInus.  The branch is taken if 
  612.                the   N-bit  is  1.   This  instruction   is   the 
  613.                counterpart of the BPL-instruction.
  614.           BVC: where VC stands for oVerflow Clear.  The branch is 
  615.                taken if the V-bit is 0. This instruction is often 
  616.                used after an Integer Arithmetic instruction  like 
  617.                ADD, SUB, MUL etc.
  618.           BVS: where  VS stands for oVerflow Set.  The branch  is 
  619.                taken if the V-bit is 1.  This instruction is  the 
  620.                counterpart of the BVC-instruction.
  621.           BRA: where   RA   stands  for   bRanch   Always.   This 
  622.                instruction is often used at the end of a loop  to 
  623.                go back to the beginning of the loop.
  624.          Branches often used after an arithmetic operation on
  625.          two's complement numbers.
  626.           BGE: where GE stands for Greater or Equal.  This branch 
  627.                is  taken  if the N and V-bits  contain  the  same 
  628.                value.
  629.  
  630.           BGT: where GT stands for Greater Than.  This branch  is 
  631.                taken in the following cases:
  632.                - N is 1, V is 1, Z is 0
  633.                - N is V is Z is 0
  634.           BLE: where LE stands for Lower or Equal. This branch is 
  635.                taken in the following cases:
  636.                - Z is 1
  637.                - N and V-bits contain different values
  638.           BLT: where  LT  stands for Less Than.  This  branch  is 
  639.                taken  if  the  N  and  V-bits  contain  different 
  640.                values.
  641.          Brances often used after an arithmetic operation on
  642.          unsigned numbers.
  643.           BHI: where HI stands for HIgher.  This branch is  taken 
  644.                if the N and V-bits contain the same value.
  645.           BLS: where LS stands for Lower or Same.  This branch is 
  646.                taken  if  the  C  and  Z-bits  contain  different 
  647.                values.
  648. Example:
  649.           This  shows  a piece of a C-program and  an  equivalent 
  650.           piece  of  a PASCAL-program which are  translated  into 
  651.           assembler. (variabele is signed)
  652.           C:
  653.                if (variable == 1 || variable > 4) variable = 5;
  654.                else var *= 3;
  655.           PASCAL:
  656.                if (variable == 1) or (variable > 4)
  657.                then variable := 5
  658.                else variable := variable * 3
  659.  
  660.           * Most assemblers will optimize the branch-instructions
  661.           * to the short forms
  662.                  CMP.W   #1,variable
  663.                  BEQ     L10000
  664.                  CMP.W   #4,variable
  665.                  BLE     L2
  666.           L10000:
  667.                  MOVE.W  #5,variable
  668.                  BRA     L3
  669.           L2:
  670.                  MOVE.W  variable,R0
  671.                  MULS    #3,R0
  672.                  MOVE.W  R0,variable
  673.           L3:
  674. Instructions:  DBcc  (cc stands for Condition Code)
  675. Syntax:        DBcc  Dn,<address>
  676. Data sizes:    byte  or word.  This implicates that  the  branch-
  677.                instructions can branch in an area of 32K.  Dn  is 
  678.                considered to contain a word.
  679. Condition codes affected:
  680.                None
  681. Function:
  682.           The  group of Decrement and Branch (DBcc)  instructions 
  683.           provide  an efficient way of creating loops.  They  are 
  684.           nearly  always placed at the end of a loop.  First  the 
  685.           condition   is  tested,   then  the   dataregister   is 
  686.           decremented.  The  branch  is taken  in  the  following 
  687.           cases:
  688.           - Dn is -1;
  689.           - The condition cc in DBcc is satisfied.
  690.           There  are 16 possible variations of this  instruction. 
  691.           They  all are nearly the same as the  Bcc-instructions, 
  692.           with two exceptions. These are:
  693.           DBF or DBRA:
  694.                This  loop can only be terminated by  count  since 
  695.                the other condition can never be satisfied.
  696.           DBT: Only performs a decrement on the dataregister  and 
  697.                never branches.  To me this seems a pretty useless 
  698.                instruction,  which is only there to make the DBcc 
  699.                series logically complete.
  700. Example:
  701.           This  piece of code is an efficient  implementation  of 
  702.           the strcpy-function of the C-language.  A0 contains the 
  703.           address  of  the  source string  and  A1  contains  the 
  704.           address  of the destination string.  In C the end of  a 
  705.           string is marked by a byte containing 0.
  706.                  MOVE.W  #$ffff,D0
  707.           LOOP:  MOVE.B  (A0)+,(A1)+
  708.                  DBEQ    D0,LOOP
  709.           This  piece of code can easily be transformed into  the 
  710.           strncpy-function  by  loading D0 with  the  appropriate 
  711.           value.
  712.  
  713.  
  714. Instructions:  Scc  (cc stands for Condition Code)
  715. Syntax:        Scc  <address>
  716. Data sizes:    byte.
  717.  
  718. Condition codes affected:
  719.                None
  720. Function: Sets a byte to $ff if the condition codes satisfie.  If 
  721.           the  condition is not satisfied the byte is set  to  0. 
  722.           This  group of 16 instructions is rarely  used.  Nearly 
  723.           all forms are the same as the DBcc group except for the 
  724.           following two instructions:
  725.           SF:  the same as a CLR.B instruction
  726.           ST:  the same as a MOVE.B #$ff, <address>
  727. Example:
  728.           Be inventive, invent one yourself!
  729.  
  730.  
  731. Instruction:   BSR, JSR
  732. Syntax:        BSR  <address>
  733.                JSR  <address>
  734. Data sizes:    none
  735. Condition codes affected:
  736.                none
  737.  
  738.  
  739.  
  740. Addressing modes allowed (only for JSR):
  741. Destination:
  742.           (An)
  743.           w(An)
  744.           b(An,Rn)
  745.           w
  746.           l
  747.           w(PC)
  748.           b(PC,Rn)
  749. Function: The  BSR  (Branch  to  SubRoutine)  and  JSR  (Jump  to 
  750.           SubRoutine)   instructions   are   used   for   calling 
  751.           subroutines.  BSR  can branch in a range  of  32K.  JSR 
  752.           should  be  used when a jump out of the  32K  range  is 
  753.           needed.   Some   assemblers  optimize  JSR   into   BSR 
  754.           instructions  whenever  possible,  since  BSR  is  more 
  755.           efficient   than   JSR.   When  executing   a   BSR/JSR 
  756.           instruction,  the  68000 first pushes the PC  (program-
  757.           counter) on the stack and then load the PC with the new 
  758.           address. See below for the RTS (ReTurn from Subroutine) 
  759.           instruction.
  760.  
  761.  
  762. Instruction:   RTS
  763. Syntax:        RTS
  764. Data sizes:    none
  765. Condition codes affected:
  766.                none
  767. Function: Counterpart  of BSR/JSR instructions.  Reloads  the  PC 
  768.           with  the value on top of the stack.  This  value  will 
  769.           nearly  always have been put on top of the stack  by  a 
  770.           BSR/JSR instruction.
  771. Example:  * the strcpy function discussed before
  772.           STRCPY:
  773.                  MOVE.W  #$FFFF,D0
  774.           LOOP:  MOVE.W  (A0)+,(A1)+
  775.                  DBEQ    D0, LOOP
  776.                  RTS
  777.           * some other code
  778.           BEGIN:
  779.                  MOVE.L  #SOURCE,A0
  780.                  MOVE.L  #DEST,A1
  781.                  JSR     STRCPY
  782.                  RTS
  783.  
  784.           * the strings are put in a data area
  785.           .DATA
  786.           * 80 bytes for every string
  787.           SOURCE .DS.B   80
  788.           DEST   .DS.B   80
  789.           * .DS.B means Define Storage Byte
  790.           * so 80 bytes are define as storage for each string
  791.  
  792.  
  793. Instruction:   JMP
  794. Syntax:        JMP  <ea>
  795. Data sizes:    none
  796. Condition codes affected:
  797.                none
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806. Addressing modes allowed:
  807. Destination:
  808.           (An)
  809.           w(An)
  810.           b(An,Rn)
  811.           w
  812.           l
  813.           w(PC)
  814.           b(PC,Rn)
  815. Function: Transfer program control to another address.  The PC is 
  816.           loaded  with the specified address.  In fact this is  a 
  817.           variant  of  the MOVE instruction.  In  this  case  the 
  818.           destination register is inherently defined,  namely the 
  819.           PC-register.  Therefore we could translate JMP <ea>  to 
  820.           MOVE.L <ea>,PC .
  821.  
  822. Instruction:   RTR
  823. Syntax:        RTR
  824. Data sizes:    none
  825. Condition codes affected:
  826.                none
  827.  
  828. Function: ReTurn    and   Restore.    Counterpart   of    BSR/JSR 
  829.           instructions.  Reloads the PC with the value on top  of 
  830.           the stack.  This value will nearly always have been put 
  831.           on top of the stack by a BSR/JSR instruction.  The only 
  832.           difference  with the RTS is that with this  instruction 
  833.           also  the CCR is reloaded.  This instruction is  rarely 
  834.           used  but  comes  in  handy when  one  doesn't  want  a 
  835.           subroutine to influence the condition codes. Before the 
  836.           JSR instruction you should use the instruction:
  837.           MOVE.B CCR,-(A7)
  838.           which pushes the CCR on the stack
  839.  
  840. Next time:  The last part of the instruction set.  These are  the 
  841. instructions  which can only be executed when supervisor-mode  is 
  842. active.
  843.  
  844.  
  845.  
  846. ASSEMBLY LANGUAGE COURS PART IX by Mark van den Boer
  847.  
  848. System Control instructions
  849.  
  850. In this part of the course the last group of instructions will be 
  851. explained.  This group of instructions deals with the supervisor-
  852. mode  and are therefore sometimes referred to as  system  control 
  853. instructions.  To  remind  you:  the  S-bit  in  the  SR  (status 
  854. register)  of  the  68000  determines whether  the  68000  is  in 
  855. supervisor-mode or not.  Many of these instructions deal with  so 
  856. called  exceptions.  Exception is another word for interrupt  and 
  857. these are used to force program control immediately to a specific 
  858. routint exception handler routine.  Exceptions are used to detect 
  859. situations  that  are  urgent and need to  be  handled  directly. 
  860. Therefore  every  exception has a vector  assigned  to  it.  This 
  861. vector is a pointer to a routine which performs some action which 
  862. should  be  taken when such an exception  occurs.  The  exception 
  863. vectors are located in the first 256 longwords of memory.
  864.  
  865.  
  866.  
  867.  
  868. Instruction:   CHK
  869. Syntax:        CHK <ea>,Dn
  870. Data sizes:    word
  871. Condition codes affected:
  872.                X    not affected
  873.                N    Set if Dn is less than zero,  cleared if <ea> 
  874.                     less than Dn, in all other cases undefined
  875.                Z
  876.                V
  877.                C    always undefined
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890. Addressing modes allowed:
  891. Source:
  892.           Dn
  893.           (An)
  894.           (An)+
  895.           -(An)
  896.           w(An)
  897.           b(An,Rn)
  898.           w
  899.           l
  900.           w(PC)
  901.           b(PC,Rn)
  902.           #
  903. Destination:
  904.           Dn
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912. Function: Compares the contents of the effective address  operand 
  913.           with  the data-register.  If the data register is  less 
  914.           than zero (the data register is always considered to be 
  915.           a  signed word) or greater than the contents  of  <ea>, 
  916.           then   an  exception  occurs.   The  pointer  to   this 
  917.           exception-routine  is  located  at  address  $18.  This 
  918.           instruction  is  used to check if a  data  register  is 
  919.           within  a  range.   It  is  often  used  by  high-level 
  920.           languages   such  as  PASCAL  to  perform   array-bound 
  921.           checking.
  922. Examples:
  923. Instruction              Before              After
  924. CHK #50,D0               D0=45               D0=45
  925.                          No exception occured, if D0 had been 51
  926.                          or greater then an exception would have
  927.                          occured.
  928.  
  929.  
  930. Instruction:   MOVE USP       (privileged instruction)
  931. Syntax:        MOVE USP,An    or   MOVE An,USP
  932. Data sizes:    long
  933.  
  934. Condition codes affected:
  935.                X
  936.                N
  937.                Z
  938.                V
  939.                C    not affected
  940. Addressing modes allowed:
  941.      See syntax
  942. Function: As  you all should know,  the 68000 has in fact two  A7 
  943.           registers.  One A7 register is used when in supervisor-
  944.           mode,  the  other  when in usermode (this  is:  not  in 
  945.           supervisor-mode.   It  is  sometimes  desirable  for  a 
  946.           program  which is executing in supervisor mode to  know 
  947.           the value of the usermode A7-register. This instruction 
  948.           provides  a  way to obtain and change the value  of  A7 
  949.           usermode-register.
  950. Example:
  951. Instruction              Before              After
  952. MOVE USP,A6              A7user=$12345678    A7user=$12345678
  953.                          A6    =$00000000    A6    =$12345678
  954.                          A7sup =$87654321    A7sup =$87654321
  955.  
  956. Instruction:   RESET
  957. Syntax:        RESET          (privileged instruction)
  958. Data sizes:    none
  959. Condition codes affected:
  960.                X
  961.                N
  962.                Z
  963.                V
  964.                C    not affected
  965. Function: Reset all external devices. A device can be a chip like 
  966.           the 6850.
  967.  
  968.  
  969. Instruction:   RTE            (privileged instruction)
  970. Syntax:        RTE
  971. Data sizes:    none
  972. Condition codes affected:
  973.                none
  974.  
  975.  
  976.  
  977.  
  978. Function: Every exception is terminated by this  instruction.  It 
  979.           can be compared to RTS. The only difference is that RTE 
  980.           will  restore  the  SR  in  addition.   Note  that   an 
  981.           exception-routine   has  the  responsibility  to   save 
  982.           registers if this is important.
  983.  
  984.  
  985. Instruction:   STOP           (privileged instruction)
  986. Syntax:        STOP #
  987. Data sizes:    word
  988. Condition codes affected:
  989.           All set as a direct result of the operand
  990. Addressing modes allowed:
  991. Source:
  992.           #
  993. Function: Stop execution of a program until an exception  occurs. 
  994.           The  operand  stored  in the SR.  Note  that  with  the 
  995.           operand  a minimum interrupt level can  be  determined. 
  996.           With  this  instruction it is possible to  wait  for  a 
  997.           videochip interrupt to occur.
  998.  
  999.  
  1000. Example:
  1001.           STOP #%0010011000011111
  1002.           Wait  for  an exception with a priority of 6  or  7  to 
  1003.           occur and set the XNZVC-bits.
  1004.  
  1005.  
  1006. Instruction:   TRAP
  1007. Syntax:        TRAP #
  1008. Data sizes:    # must be >=0 and <=15
  1009. Condition codes affected:
  1010.                X
  1011.                N
  1012.                Z
  1013.                V
  1014.                C    not affected
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022. Function: This  instruction generates an exception.  The  operand 
  1023.           indicates  an  exception number.  The vectors  for  the 
  1024.           exceptions  are located at addresses $80 to  $BC.  This 
  1025.           instruction  is  mainly  used to  allow  programs  that 
  1026.           execute in user-mode to call supervisor-mode  routines. 
  1027.           This  way  a  user can be given a  number  of  specific 
  1028.           functions. In the ST trap vectors 2, 14 and 15 are used 
  1029.           for GEM,  BIOS and XBIOS functions.  In the case of the 
  1030.           ST  the TRAP-instructions is preceded  by  instructions 
  1031.           that  put  function numbers and  parameters  for  these 
  1032.           functions  on  the stack.  This way it is  possible  to 
  1033.           assign groups of functions to one trap-vector.
  1034.           Note  that  when  calling  a  TRAP  in  user-mode   the 
  1035.           stackpointers  change (supervisor-mode  and  user-mode, 
  1036.           remember??). Thus, the MOVE USP instruction can be used 
  1037.           to retrieve parameters that had been put on the stack.
  1038.  
  1039.  
  1040. Instruction:   TRAPV
  1041. Syntax:        TRAPV
  1042. Data sizes:    none
  1043.  
  1044. Condition codes affected:
  1045.                none
  1046. Function: When  the  V-bit  is  set  an  exception  occurs.   The 
  1047.           exception vector is located at address $1C. When the V-
  1048.           bit is clear nothing happens.  This instruction can  be 
  1049.           used by high-level languages to inform the user that an 
  1050.           overflow error has occured.
  1051.  
  1052.  
  1053.  
  1054. MC 68000 ASSEMBLY LANGUAGE COURSE PART X by Mark van den Boer
  1055.  
  1056. Now  all instructions of the 68000 have been explained it's  time 
  1057. to  bring  this knowledge into practice.  This last part  of  the 
  1058. course  will  deal  with the subject  of  translating  high-level 
  1059. constructs to the equivalent assembler constructs. The C program-
  1060. ming  language will be used as the high-level language which  has 
  1061. to  be translated to assembler.  A note to those of you  who  are 
  1062. more familiar with Pascal or BASIC:  litte imagination is needed 
  1063. to deduct the similar constructs in Pascal and BASIC.
  1064. What now follows is a C-program which containing several commonly 
  1065. used data- and control structures.  The examples show you how  to 
  1066. translate these structures into assembler.
  1067. There  should  also be a file called M68000.DOC on your  ST  NEWS
  1068. disk.  This  file contains a quick-reference card containing  all 
  1069. instructions  and allowed addressing modes.  This reference  card 
  1070. has been made by Trustware,  Inc.  (an unregistered trademark  of 
  1071. Victor Langeveld).  I am very grateful to Victor for allowing  me 
  1072. to  include  this card,  since it's a rather tedious job  to  put 
  1073. together such a card.  One thing's for sure:  this card is one of 
  1074. the  better of its kind and it's the most compact reference  card 
  1075. for the 68000 I've ever seen.
  1076. /*
  1077.         A function. (Called a procedure or function in Pascal
  1078.         and a subroutine in BASIC)
  1079.         Note how parameters are passed in the assembly language
  1080.         translation.
  1081.         Als pay attention to how local variables are stored.
  1082. */
  1083. int function (number, pointer)
  1084. int     number;
  1085. char    *pointer;
  1086. {
  1087.         register int    i, j;
  1088.         char    *c;
  1089.  
  1090.         i = number - 1;
  1091.         c = pointer;
  1092.         c = "new Queensryche album: Operation Mindcrime";
  1093.         /* Note how a string is stored */
  1094.         return i;          /* Note how a value is returned */
  1095. }
  1096.  
  1097.  
  1098. .text
  1099. function:
  1100. * offset of number = 8
  1101. * offset of pointer = 10
  1102.         LINK    A6,#-4
  1103. * save registers
  1104.         MOVEM.L D6-D7,-(sp)     * sp = A7
  1105. * i in D7
  1106. * j in D6
  1107. * offset of c = -4
  1108. * i = number - 1
  1109.         MOVE.W  8(A6),D7
  1110.         SUB.W   #1,D7
  1111. * c = pointer
  1112.         MOVE.L  10(A6),-4(A6)
  1113. * c = "new Queensryche album: Operation Mindcrime"
  1114. .data
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120. L2:
  1121.         .dc.b 'new Queensryche album: Operation Mindcrime',0
  1122.         MOVE.L  #L2,-4(A6)
  1123. * D0 is used for resulting values from functions
  1124. * return i; D0 is always used for the result of a function
  1125.         MOVE.W  D7,D0
  1126. * restore registers
  1127.         MOVEM.L (sp)+,D6-D7
  1128.         UNLK    A6
  1129. * }
  1130.         RTS
  1131.  
  1132. * global variables
  1133. .bss
  1134. * int     i;
  1135. i:      .ds.w   1
  1136. * char    character
  1137. character:      .ds.b   1
  1138. * int     *i_pointer
  1139. i_pointer:      .ds.l   1
  1140.  
  1141.  
  1142. * int     i_array[10][5]
  1143. i_array:        .ds.w   10*5
  1144. * one struct is in fact 3 bytes long, but for every structure
  1145. * 4 bytes are reserved. This is because the 68000 can only
  1146. * address words at even addresses.
  1147. * struct { /* this is the equivalent of a record in PASCAL */
  1148. *         int     i;
  1149. *         char    c;
  1150. * } structure[5];
  1151. structure:      .ds.b   4*5
  1152.  
  1153.  
  1154. main()
  1155. {
  1156.         /* assignment of a constant to a variable */
  1157.         i = 9;
  1158.  
  1159.         /* assignment of a constant to a variable */
  1160.         character = 'c';
  1161.  
  1162.         /* assignment of a constant to a variable */
  1163.         i_pointer = &i;
  1164.         /* watch how indexing of array is done */
  1165.         /* integer is 2 bytes, so the address of
  1166.            array-element [3][4] is:
  1167.            (3 * 5 (the length of i_array[3]) + 4)
  1168.            * 2 (size of an integer) = 38.
  1169.            So the integer should be stored at i_array + 38.
  1170.         */
  1171.         i_array[3][4] = *i_pointer;
  1172.  
  1173.         /* Now the distance in bytes from the beginning of the
  1174.            array must be computed during program execution,
  1175.            in contrary to the previous example.
  1176.         */
  1177.         i_array[i][i - 1] = 2;
  1178.  
  1179.         /* Assignments to arrays of structures */
  1180.         structure[1].i = 3;
  1181.         structure[i].c = character;
  1182.  
  1183.         /* expression evaluation and assignment */
  1184.         i = i_array[0][0] * i_array[0][1] +
  1185.             i_array[0][2] / i_array[0][3];
  1186.         /* conditional statement */
  1187.         if (i < i_array[i][i]) i = 1;
  1188.         else i = 2;
  1189.  
  1190.         /* while loop */
  1191.         while (i <= 10) i++;
  1192.         /* continue and break statements */
  1193.         while (i++ <= 10) {
  1194.                 if (i != 4) continue;
  1195.                 else break;
  1196.         }
  1197.  
  1198.         /* for loop */
  1199.         for (i = 4; i >= 0; i--) i_array[i][i] = i;
  1200.  
  1201.         /* do loop */
  1202.         do i++; while (i < 10 && i != 5);
  1203.  
  1204.         /* switch statement; watch the application of a
  1205.            jump-table. Pay special attention to how 'case 4'
  1206.            which must 'default' is solved.
  1207.  
  1208.         */
  1209.         switch (i) {
  1210.         case 0:
  1211.                 i = 0;
  1212.                 break;
  1213.         case 1:
  1214.                 i = 5;
  1215.                 break;
  1216.         case 2:
  1217.         case 3:
  1218.                 i = 7;
  1219.                 break;
  1220.         case 5:
  1221.                 i = 1;
  1222.                 break;
  1223.         default:
  1224.                 i = 2;
  1225.                 break;
  1226.         }
  1227.  
  1228.  
  1229.  
  1230.         /* switch statement;
  1231.            watch how 'case 999' has destroyed the
  1232.            jumptable-optimization.
  1233.         */
  1234.         switch (i) {
  1235.         case 0:
  1236.                 i = 0;
  1237.                 break;
  1238.         case 1:
  1239.                 i = 5;
  1240.                 break;
  1241.         case 2:
  1242.         case 3:
  1243.                 i = 7;
  1244.                 break;
  1245.         case 5:
  1246.                 i = 1;
  1247.                 break;
  1248.         case 999:
  1249.                 /* This case should be tested seperately so
  1250.                    the assembler code can be more efficient.
  1251.  
  1252.                 */
  1253.                 i = 100;
  1254.                 break;
  1255.         default:
  1256.                 i = 2;
  1257.                 break;
  1258.         }
  1259.  
  1260.         /* manipulating bits */
  1261.         i = i & 0x2345;
  1262.         i = i | 0x2345;
  1263.         i = i ^ 0x2345;
  1264.         i = ~i;
  1265.         i <<= i;
  1266.  
  1267.         /* using the result of a function */
  1268.         i = function(5, &character);
  1269.  
  1270. }
  1271.  
  1272. .text
  1273.  
  1274. main:
  1275. * Reserve 4 bytes. This way, when the first parameter for a
  1276. * function is pushed onto the stack, no pre-decrementing of sp
  1277. * has to be done. This 'trick' is used by the DRI-C-compiler.
  1278.         LINK    A6,#-4
  1279. * i = 9
  1280.         MOVE.W  #9,i
  1281. * character = 'c'
  1282.         MOVE.B  #'c,character
  1283. * i_pointer = &i
  1284.         MOVE.L  #i,i_pointer
  1285. * i_array[3][4] = *i_pointer
  1286.         MOVE.L  i_pointer,A0
  1287.         MOVE.W  (A0),38+i_array
  1288. * i_array[i][i - 1] = 2
  1289.         MOVE.W  i,D0     * compute byte offset from first element
  1290.         MULS    #10,D0
  1291.         MOVE.W  i,D1
  1292.         SUB.W   #1,D1
  1293.         ASL.W   #1,D1   * multiply by 2 because an int is 2 bytes
  1294.         EXT.L   D1
  1295.         ADD.L   D1,D0
  1296.         ADD.L   #i_array,D0
  1297.         MOVE.L  D0,A0   * move computed address to address-reg
  1298.         MOVE.W  #2,(A0)
  1299. * structure[1].i = 3
  1300.         MOVE.W  #3,4+structure
  1301. * structure[i].c = character
  1302.         MOVE.W  i,A0
  1303.         ADD.L   A0,A0
  1304.         ADD.L   A0,A0
  1305. * Two ADD operations are faster than a MUL and a MOVE
  1306.         ADD.L   #structure,A0
  1307.         MOVE.B  character,2(A0)
  1308. * i = i_array[0][0] * i_array[0][1] +
  1309. *     i_array[0][2] / i_array[0][3];
  1310.         MOVE.W  i_array,D0
  1311.         MULS    2+i_array,D0
  1312.         MOVE.W  4+i_array,D1
  1313.         EXT.L   D1
  1314.         DIVS    6+i_array,D1
  1315.         ADD.W   D1,D0
  1316.         MOVE.W  D0,i
  1317. * if (i < i_array[i][i]) i = 1;
  1318. * else i = 2;
  1319.         MOVE.W  i,D0
  1320.         MULS    #10,D0
  1321.         MOVE.W  i,D1
  1322.         ASL.W   #1,D1
  1323.         EXT.L   D1
  1324.         ADD.L   D1,D0
  1325.         MOVE.L  D0,A0
  1326.         MOVE.L  #i_array,A1
  1327.         MOVE.W  0(A0,A1.L),D0
  1328.         CMP     i,D0
  1329.         BLE     L4
  1330. * i = 1
  1331.         MOVE.W  #1,i
  1332.         BRA     L5
  1333. L4:
  1334. * i = 2
  1335.         MOVE.W  #2,i
  1336. L5:
  1337.         BRA     L8
  1338.  
  1339.  
  1340. * while (i <= 10) i++;
  1341. * This loop has been optimized:
  1342. * one BRA instruction was saved by putting the test after the
  1343. * do-part. The label L5:    BRA L8    takes care that the while
  1344. * condition is executed first at the beginning of the loop
  1345. L7:
  1346.         ADD.W   #1,i
  1347. L8:
  1348.         CMP.W   #10,i
  1349.         BLE     L7
  1350. * while (i++ <= 10) {
  1351. *   if (i != 4) continue;
  1352. *   else break;
  1353. * }
  1354. L6:
  1355.         BRA     L11
  1356. L10:
  1357.         CMP.W   #4,i
  1358.         BNE     L11
  1359.         BRA     L9
  1360.  
  1361.  
  1362. L11:
  1363.         CMP     #10,i
  1364.         MOVE.W  SR,D0   * save condition codes
  1365.         ADD.W   #1,i
  1366.         MOVE    D0,CCR  * and restore
  1367.         BLE     L10
  1368. * for (i = 4; i >= 0; i--) i_array[i][i] = i
  1369. L9:
  1370.         MOVE.W  #4,i
  1371.         BRA     L14
  1372. L15:
  1373.         MOVE.W  i,D0
  1374.         MULS    #10,D0
  1375.         MOVE.W  i,D1
  1376.         ASL.W   #1,D1
  1377.         EXT.L   D1
  1378.         ADD.L   D1,D0
  1379.         ADD.L   #i_array,D0
  1380.         MOVE.L  D0,A0
  1381.         MOVE.W  i,(A0)
  1382. L13:
  1383.         SUB.W   #1,i
  1384. L14:
  1385.         TST     i
  1386.         BGE     L15
  1387. L12:
  1388. * do i++; while (i < 10 && i != 5);
  1389. L18:
  1390.         ADD.W   #1,i
  1391. L17:
  1392.         CMP.W   #10,i
  1393.         BGE     L10000
  1394.         CMP.W   #5,i
  1395.         BNE     L18
  1396. L10000:
  1397. * switch (i) {
  1398. * case 0:
  1399. *        i = 0;
  1400. *        break;
  1401. * case 1:
  1402. *        i = 5;
  1403. *        break;
  1404.  
  1405.  
  1406. * case 2:
  1407. * case 3:
  1408. *        i = 7;
  1409. *        break;
  1410. * case 5:
  1411. *        i = 1;
  1412. *        break;
  1413. * default:
  1414. *        i = 2;
  1415. *        break;
  1416. * }
  1417. L16:
  1418.         MOVE.W  i,D0
  1419.         BRA     L20
  1420. L21:
  1421.         CLR.W   i
  1422.         BRA     L19
  1423. L22:
  1424.         MOVE.W  #5,i
  1425.         BRA     L19
  1426.  
  1427.  
  1428. L23:
  1429. L24:
  1430.         MOVE.W  #7,i
  1431.         BRA     L19
  1432. L25:
  1433.         MOVE.W  #1,i
  1434.         BRA     L19
  1435. L26:
  1436.         MOVE.W  #2,i
  1437.         BRA     L19
  1438. L20:
  1439. * Test if i is in case-range
  1440. * if not goto default
  1441. * if in range compute address to jump to
  1442.         CMP.W   #5,D0
  1443.         BHI     L26
  1444.         ASL.W   #2,D0
  1445.         MOVE.W  D0,A0
  1446.         ADD.L   #L27,A0
  1447.         MOVE.L  (A0),A0
  1448.         JMP     (A0)
  1449. .data
  1450. L27:
  1451. * jumptable
  1452. .dc.l L21
  1453. .dc.l L22
  1454. .dc.l L23
  1455. .dc.l L24
  1456. .dc.l L26
  1457. .dc.l L25
  1458. .text
  1459. L19:
  1460. * switch (i) {
  1461. * case 0:
  1462. *         i = 0;
  1463. *         break;
  1464. * case 1:
  1465. *         i = 5;
  1466. *         break;
  1467. * case 2:
  1468. * case 3:
  1469. *         i = 7;
  1470. *         break;
  1471.  
  1472. * case 5:
  1473. *         i = 1;
  1474. *         break;
  1475. * case 999:
  1476. *         /* This case should be tested seperately so
  1477. *            the assembler code can be more efficient.
  1478. *         */
  1479. *         i = 100;
  1480. *         break;
  1481. * default:
  1482. *         i = 2;
  1483. *         break;
  1484. * }
  1485.         MOVE.W  i,D0
  1486.         BRA     L29
  1487. L30:
  1488.         CLR.W   i
  1489.         BRA     L28
  1490. L31:
  1491.         MOVE.W  #5,i
  1492.         BRA     L28
  1493.  
  1494. L32:
  1495. L33:
  1496.         MOVE.W  #7,i
  1497.         BRA     L28
  1498. L34:
  1499.         MOVE.W  #1,i
  1500.         BRA     L28
  1501. L35:
  1502.         MOVE.W  #100,i
  1503.         BRA     L28
  1504. L36:
  1505.         MOVE.W  #2,i
  1506.         BRA     L28
  1507.         BRA     L28
  1508. L29:
  1509.         EXT.L   D0
  1510.         MOVE.L  #L37,A0
  1511.         MOVE.W  #6,D1
  1512.  
  1513.  
  1514.  
  1515.  
  1516. L38:
  1517.         CMP.L   (A0)+,D0
  1518.         DBEQ    D1,L38
  1519.         MOVE.L  24(A0),A0
  1520.         JMP     (A0)
  1521. .data
  1522. L37:
  1523. * table of case values
  1524. .dc.l 0
  1525. .dc.l 1
  1526. .dc.l 2
  1527. .dc.l 3
  1528. .dc.l 5
  1529. .dc.l 999
  1530. .dc.l 0
  1531. * jump table
  1532. .dc.l L30
  1533. .dc.l L31
  1534. .dc.l L32
  1535. .dc.l L33
  1536. .dc.l L34
  1537. .dc.l L35
  1538. .dc.l L36
  1539. .text
  1540. L28:
  1541. * i = i & 0x2345
  1542.         ANDI.W  #$2345,i
  1543. * i = i | 0x2345
  1544.         ORI.W   #$2345,i
  1545. * i = i ^ 0x2345
  1546.         EORI.W  #$2345,i
  1547. * i = ~i
  1548.         NOT.W   i
  1549. * i <<= i
  1550.         MOVE.W  i,D1
  1551.         MOVE.W  D1,D0
  1552.         ASL.W   D1,D0
  1553.         MOVE.W  D0,i
  1554. * i = function (5, &character)
  1555.         MOVE.W  #51,(sp)
  1556.         MOVE.L  #character,-(sp)
  1557.         ADDQ.L  #4,sp
  1558.         MOVE.W  D0,i
  1559.  
  1560. L3:
  1561.         UNLK    A6
  1562.         RTS
  1563.  
  1564.  
  1565. Hope you had much fun with this course and that you learnt a lot!